﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;

namespace RXY_Mall.Admin.Authorize
{
    public class UserCookie
    {
        /// <summary>
        /// 用户Token
        /// </summary>
        public string Token { get; set; }

        /// <summary>
        /// Id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 上次登录时间
        /// </summary>
        public string LastloginTime { get; set; }

        public static UserCookie GetCurrentUser()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return null;
            }
            var userCookie = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
            var serializer = new JavaScriptSerializer();
            return serializer.Deserialize<UserCookie>(userCookie);
        }

        public static int GetCurrentUserId()
        {
            var userCookie = GetCurrentUser();
            if (userCookie != null)
            {
                return userCookie.Id;
            }
            return 0;
        }
        public static string GetCurrentUserName()
        {
            var userCookie = GetCurrentUser();
            if (userCookie != null)
            {
                return userCookie.UserName;
            }
            return "";
        }
    }
}